
var default_perpage = 0;
$(document).ready(function () {
	$().UItoTop({
		easingType : "easeOutQuart"
	});
	$("#caledar_from").datepicker({
		changeMonth : true,
		changeYear : true,
		dateFormat : "dd-mm-yy",
		yearRange : "1930:2050",
	});
	$("#caledar_to").datepicker({
		changeMonth : true,
		changeYear : true,
		dateFormat : "dd-mm-yy",
		yearRange : "1930:2050",
	});
	
	$(".custom_chk").jqTransCheckBox();
	$(".custom_rd").jqTransRadio();
	$(".custom_select").jqTransSelect();
	$(".fancyboxClick").fancybox();
	$("#saveContent").click(function () {
		$("#loader").fadeIn()
	});
	var url = $.url(document.location.href);
	if (url.segment(-1) != "update" && url.segment(-2) != "update") {
		if (url.fsegment(1) == "back" || url.fsegment(1) == "save") {
			if ($("#start").val() == "") {
				$("#start").val(0)
			}
			searchContent($("#start").val(), default_perpage);
		} else {
			if (url.segment(-1) != "update_profile") {
				if (module != "admincp") {
					searchContent(0, default_perpage);
				}
			}
		}
	}
});

function show_perm_denied() {
	$(".gr_perm_error").width($(".right_content").width() - 2);
	$(".gr_perm_error").fadeIn(500);
	$("#loader").fadeOut(300);
	$(".table").css("marginTop", 4);
	setTimeout("$('.gr_perm_error').fadeOut(300); $('.table').css('marginTop',0);", 4000);
}

function enterSearch(a) {
	if (a.keyCode == 13) {
		searchContent(0)
	}
}
function sort(func) {
	var func_sort = $("#func_sort").val();
	var type_sort = $("#type_sort").val();
	if (func == func_sort) {
		if (type_sort == "DESC") {
			$("#type_sort").val("ASC")
		} else {
			$("#type_sort").val("DESC")
		}
	} else {
		$("#func_sort").val(func);
		$("#type_sort").val("DESC")
	}
	searchContent(0, $("#per_page").val())
}
function updateStatus(id, status, module) {
	var url = root + "admincp/" + module + "/ajaxUpdateStatus";
	$.post(url, {
		id : id,
		status : status
	}, function (data) {
		$("#loadStatusID_" + id).html(data)
	})
}
function selectItem(id) {
	var itemCheck = document.getElementById("item" + id);
	if (itemCheck.checked == false) {
		$(".item_row" + id).addClass("row_active")
	} else {
		$(".item_row" + id).removeClass("row_active")
	}
}
function selectAllItems(max) {
	if (document.getElementById("selectAllItems").checked == false) {
		$(".jqTransformCheckboxWrapper a").addClass("jqTransformChecked");
		for (var i = 0; i < max; i++) {
			if (document.getElementById("item" + i) != null) {
				$(".item_row" + i).addClass("row_active");
				itemCheck = document.getElementById("item" + i);
				itemCheck.checked = true;
			}
		}
	} else {
		$(".jqTransformCheckboxWrapper a").removeClass("jqTransformChecked");
		for (var i = 0; i < max; i++) {
			if (document.getElementById("item" + i) != null) {
				$(".item_row" + i).removeClass("row_active");
				itemCheck = document.getElementById("item" + i);
				itemCheck.checked = false;
			}
		}
	}
}
function showStatusAll() {
	var max = $("#per_page").val();
	for (var i = 0; i < max; i++) {
		if (document.getElementById("item" + i) != null) {
			if (document.getElementById("item" + i).checked == true) {
				updateStatus($("#item" + i).val(), 0, module);
			}
		}
	}
}
function hideStatusAll() {
	var max = $("#per_page").val();
	for (var i = 0; i < max; i++) {
		if (document.getElementById("item" + i) != null) {
			if (document.getElementById("item" + i).checked == true) {
				updateStatus($("#item" + i).val(), 1, module);
			}
		}
	}
}
function deleteItem(id) {
	var del = confirm("Are you sure delete item?");
	if (del) {
		var url = root + "admincp/" + module + "/delete";
		$.post(url, {
			id : id
		}, function (data) {
			if (data == "permission-denied") {
				show_perm_denied();
			} else {
				searchContent($("#start").val(), $("#per_page").val());
			}
		})
	}
}
function deleteAll() {
	var del = confirm("Are you sure delete item selected?");
	if (del) {
		var max = $("#per_page").val();
		for (var i = 0; i < max; i++) {
			if (document.getElementById("item" + i) != null) {
				if (document.getElementById("item" + i).checked == true) {
					id = $("#item" + i).val();
					var url = root + "admincp/" + module + "/delete";
					$.post(url, {
						id : id
					}, function (data) {
						if (data == "permission-denied") {
							show_perm_denied();
						} else {
							searchContent($("#start").val(), $("#per_page").val());
						}
					})
				}
			}
		}
	}
}
function chk_perm(b, a) {
	if (a != "no_access") {
		if (a == "read") {
			if ($("#read" + b).attr("checked") == "checked") {
				$("#noaccess" + b).attr("checked", true);
				$("#write" + b).attr("checked", false);
				$("#delete" + b).attr("checked", false);
				$(".custom_noaccess" + b).addClass("jqTransformChecked");
				$(".custom_write" + b).removeClass("jqTransformChecked");
				$(".custom_delete" + b).removeClass("jqTransformChecked");
			} else {
				$("#noaccess" + b).attr("checked", false);
				$(".custom_noaccess" + b).removeClass("jqTransformChecked");
			}
		} else {
			$("#read" + b).attr("checked", true);
			$("#noaccess" + b).attr("checked", false);
			$(".custom_read" + b).addClass("jqTransformChecked");
			$(".custom_noaccess" + b).removeClass("jqTransformChecked");
		}
	} else {
		if ($("#noaccess" + b).attr("checked") == "checked") {
			$("#read" + b).attr("checked", true);
			$(".custom_read" + b).addClass("jqTransformChecked");
		} else {
			$(".perm_access" + b).attr("checked", false);
			$(".custom_read" + b).removeClass("jqTransformChecked");
			$(".custom_write" + b).removeClass("jqTransformChecked");
			$(".custom_delete" + b).removeClass("jqTransformChecked");
		}
	}
};
